﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Spotted.Web.Data.Entities;
using Spotted.Web.Data.Result;

namespace Spotted.Web.Data.DataProvider.Impl
{
    public class EfUserDataProvider : IUserDataProvider
    {
        #region Implementation of IUserDataProvider

        /// <summary>
        /// Create a User with standard registration information
        /// </summary>
        /// <param name="newUser">The object representing </param>
        /// <returns></returns>
        public CreateUserResult CreateUser(User newUser)
        {
            using (var entities = new SpottedEntities())
            {
                using (var scope = new TransactionScope())
                {
                    // check for a duplicate
                    if (entities.Users.Count(u => u.EmailAddress.ToLower() == newUser.EmailAddress.ToLower().Trim()) > 0)
                        return new CreateUserResult {ResultCode = ResultCode.DuplicateEmail};

                    try
                    {
                        // create the user (so we have the Id)
                        entities.AddToUsers(newUser);
                        entities.SaveChanges();

                        // initiate the user session
                        UserSession session = CreateUserSessionForStandardLogin(newUser.UserId);
                        entities.AddToUserSessions(session);
                        entities.SaveChanges();
                        
                        // return the result
                        return new CreateUserResult
                            {
                                UserToken = session.SessionIdentifier.ToString(),
                                ResultCode = ResultCode.Success,
                                TokenExpiration = session.Expiration
                            };
                    }
                    catch
                    {
                        scope.Dispose();        // kill the transaction
                        throw;
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private UserSession CreateUserSessionForStandardLogin(int userId)
        {
            return new UserSession
                {
                    Expiration = DateTime.Now.AddDays(30),
                    SessionIdentifier = Guid.NewGuid(),
                    UserIdentifier = userId.ToString()
                };
        }

        /// <summary>
        /// Return a complete listing of all users
        /// </summary>
        /// <returns></returns>
        public IList<User> GetAllUsers()
        {
            using (var entities = new SpottedEntities())
            {
                return entities.Users.ToList();
            }
        }

        #endregion
    }
}
